#include <reg52.h>
#define LONGPRESSTIME  1000 //2s触发长按事件

#define uint unsigned int
sbit key1=P1^2;
sbit led3 = P1^3;
sbit led4 = P1^4;
sbit led5 = P1^5;
sbit led6 = P1^6;
sbit led7 = P1^7;
typedef enum keystate {
	initstate,
	affirmstate,
	singlestate,
	repeatstate
};

unsigned char keyscan() { 
	static unsigned char keystatevalue=0;  
	static unsigned int presstime= 0;   
	unsigned char key_value;             
	unsigned char num;
	key_value=key1;               
	switch(keystatevalue) {
	case initstate:          
		if(key_value==0) {    
			keystatevalue = affirmstate;
		}
		else {
			keystatevalue = initstate;
		}
		break;
	case affirmstate:
		if(key_value == 0) {
			keystatevalue = singlestate; 
		} else {
			keystatevalue = initstate; 
		}
		break;

	case singlestate:             
		presstime++;              
		if(presstime > (LONGPRESSTIME/10)) {      
			presstime=0;
			keystatevalue = repeatstate;  
		} else if(key_value == 1) {       
			keystatevalue = initstate;
			num++;   
			if(	num == 8) {
				num=0;
			}
		}
		break;

	case repeatstate: 
		if(key_value==1) { 
			keystatevalue=initstate;
		}
		presstime++;
		if(presstime>10) {
			presstime=0;
			num++;
			if(num==8) {
				num=0;
			}
		}
		break;
	default:
		keystatevalue=initstate;
		break;
	}
	return keystatevalue;
}